package com.ybb.production.controller;

import com.ybb.production.domain.GarmentProductionUserPay;
import com.ybb.production.domain.GarmentProductionUserPayTemplate;
import com.ybb.production.dto.*;
import com.ybb.production.responce.GarmentPayByCntResp;
import com.ybb.production.responce.GarmentPayDetailResp;
import com.ybb.production.responce.GarmentPayStyleCodeAndProcessResp;
import com.ybb.production.service.IGarmentProductionUserPayService;
import com.ybb.production.service.IGarmentProductionUserPayTemplateService;
import com.yubb.common.annotation.Log;
import com.yubb.common.core.controller.BaseController;
import com.yubb.common.core.domain.AjaxResult;
import com.yubb.common.core.domain.R;
import com.yubb.common.core.page.TableDataInfo;
import com.yubb.common.enums.BusinessType;
import com.yubb.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户工资发放Controller
 *
 * @author dj
 * @date 2023-09-27
 */
@RestController
@RequestMapping("/production/pay")
@Api(tags = "用户工资发放")
public class GarmentProductionUserPayController extends BaseController {
    @Autowired
    private IGarmentProductionUserPayService garmentProductionUserPayService;

    @Autowired
    private IGarmentProductionUserPayTemplateService garmentProductionUserPayTemplateService;


    @RequiresPermissions("production:pay:list")
    @GetMapping("/settlementListByQuantity")
    @ApiOperation(value = "计件工资结算列表")
    public TableDataInfo<GarmentPayByCntResp> settlementListByQuantity(GarmentProductionPayCntParam userPayParam) {
        startPage();
        List<GarmentPayByCntResp> list = garmentProductionUserPayService.settlementListByQuantity(userPayParam);
        return getDataTable(list);
    }

    @RequiresPermissions("production:pay:export")
    @PostMapping("/totalSalaryExport")
    @ApiOperation(value = "总工资导出")
    public void totalSalaryExport(HttpServletResponse response, GarmentProductionPayCntParam userPayParam) {
        List<GarmentPayByCntResp> list = garmentProductionUserPayService.totalSalaryExport(userPayParam);
        ExcelUtil<GarmentPayByCntResp> util = new ExcelUtil<GarmentPayByCntResp>(GarmentPayByCntResp.class);
        util.exportExcel(list, "总工资");
    }

    @RequiresPermissions("production:pay:list")
    @PostMapping("/settlementStatisticsByQuantity")
    @ApiOperation(value = "计件工资结算列表-表格上方统计")
    public R settlementStatisticsByQuantity(@RequestBody GarmentProductionPayCntParam userPayParam) {
        return R.ok(garmentProductionUserPayService.settlementStatisticsByQuantity(userPayParam));
    }

    @RequiresPermissions("production:pay:list")
    @GetMapping("/settlementDetailViewList")
    @ApiOperation(value = "计件工资结算列表-查看列表")
    public TableDataInfo<GarmentPayDetailResp> settlementDetailViewList(GarmentProductionPayDetailParam detailParam) {
        startPage();
        List<GarmentPayDetailResp> list = garmentProductionUserPayService.settlementDetailViewList(detailParam);
        return getDataTable(list);
    }


    @RequiresPermissions("production:pay:export")
    @PostMapping("/cntSalaryExport")
    @ApiOperation(value = "计件工资导出")
    public void cntSalaryExport(HttpServletResponse response, GarmentProductionPayDetailParam detailParam) {
        List<GarmentPayDetailResp> list = garmentProductionUserPayService.cntSalaryExport(detailParam);
        ExcelUtil<GarmentPayDetailResp> util = new ExcelUtil<GarmentPayDetailResp>(GarmentPayDetailResp.class);
        util.exportExcel(list, "计件工资");
    }


    @RequiresPermissions("production:pay:list")
    @PostMapping("/settlementDetailViewStatistics")
    @ApiOperation(value = "计件工资结算列表-查看列表统计")
    public R settlementDetailViewStatistics(@RequestBody GarmentProductionPayDetailParam userPayParam) {
        return R.ok(garmentProductionUserPayService.settlementDetailViewStatistics(userPayParam));
    }


    @RequiresPermissions("production:pay:add")
    @ApiOperation(value = "批量结算")
    @PostMapping("/batchSettlement")
    public AjaxResult batchSettlement(@RequestBody GarmentProductionSettleParam param) {
        return toAjax(garmentProductionUserPayService.batchSettlement(param));
    }


    @RequiresPermissions("production:pay:add")
    @ApiOperation(value = "按员工批量结算")
    @GetMapping("/batchSettlementByUser")
    public AjaxResult batchSettlementByUser(GarmentProductionPayDetailParam detailParam) {
        return toAjax(garmentProductionUserPayTemplateService.batchSettlementByUser(detailParam));
    }


    @RequiresPermissions("production:pay:add")
    @ApiOperation(value = "批量取消结算")
    @PostMapping("/batchSettlementCancel")
    public AjaxResult batchSettlementCancel(@RequestBody List<Long> ids) {
        return toAjax(garmentProductionUserPayService.batchSettlementCancel(ids));
    }


    @RequiresPermissions("production:pay:add")
    @ApiOperation(value = "修改上数")
    @PostMapping("/editUpNum")
    public AjaxResult editUpNum(@RequestBody GarmentProductionUpNumParam param) {
        return toAjax(garmentProductionUserPayService.editUpNum(param));
    }

    @RequiresPermissions("production:pay:delete")
    @ApiOperation(value = "删除上数")
    @PostMapping("/delUpNum")
    public AjaxResult delUpNum(@RequestBody GarmentProductionUpNumParam param) {
        return toAjax(garmentProductionUserPayService.delUpNum(param));
    }

    @RequiresPermissions("production:pay:list")
    @GetMapping("/settlementListByStyleCodeOrProcess")
    @ApiOperation(value = "按款号或工序结算列表")
    public TableDataInfo<GarmentPayStyleCodeAndProcessResp> settlementListByStyleCodeOrProcess(GarmentStyleCodeAndProcessParam param) {
        startPage();
        List<GarmentPayStyleCodeAndProcessResp> list = garmentProductionUserPayService.settlementListByStyleCodeOrProcess(param);
        return getDataTable(list);
    }

    @RequiresPermissions("production:pay:list")
    @GetMapping("/unSettlementCnt")
    @ApiOperation(value = "按款号或工序结算列表-未结算数量")
    public R unSettlementCnt(GarmentStyleCodeAndProcessParam param) {
        return R.ok(garmentProductionUserPayService.unSettlementCnt(param));
    }


    @RequiresPermissions("production:userPayTemplate:add")
    @Log(title = "工资发放模板", businessType = BusinessType.INSERT)
    @ApiOperation(value = "工资发放自定义项模板新增修改")
    @PostMapping("/addOrEditUserPayTemplate")
    public AjaxResult add(@RequestBody GarmentProductionUserPayTemplate garmentProductionUserPayTemplate) {
        return toAjax(garmentProductionUserPayTemplateService.insertOrUpdate(garmentProductionUserPayTemplate));
    }

    @RequiresPermissions("production:pay:add")
    @Log(title = "工资发放", businessType = BusinessType.INSERT)
    @ApiOperation(value = "工资发放")
    @PostMapping("/paySalary")
    public AjaxResult paySalary(@RequestBody List<GarmentProductionUserPay> userPayList) {
        return toAjax(garmentProductionUserPayService.paySalary(userPayList));
    }

    @RequiresPermissions("production:pay:list")
    @GetMapping("/paySalaryList")
    @ApiOperation(value = "按月发放工资-列表")
    public TableDataInfo<GarmentProductionUserPay> paySalaryList(@Valid GarmentProductionPayRecordDTO userPayParam) {
        startPage();
        List<GarmentProductionUserPay> list = garmentProductionUserPayService.paySalaryList(userPayParam);
        return getDataTable(list);
    }


    @RequiresPermissions("production:pay:list")
    @PostMapping("/paySalaryTotalStatistics")
    @ApiOperation(value = "按月发放工资-表格上方统计")
    public R paySalaryListSatistics(@RequestBody GarmentProductionPayRecordDTO userPayParam) {
        return R.ok(garmentProductionUserPayService.paySalaryListSatistics(userPayParam));
    }

    @RequiresPermissions("production:pay:cancel")
    @Log(title = "工资发放", businessType = BusinessType.INSERT)
    @ApiOperation(value = "发放记录-工资发放取消")
    @PostMapping("/paySalaryCancel")
    public AjaxResult paySalaryCancel(@RequestBody List<GarmentProductionUserPay> payRecordDTOS) {
        return toAjax(garmentProductionUserPayService.paySalaryCancel(payRecordDTOS));
    }

    @RequiresPermissions("production:pay:list")
    @GetMapping("/paySalaryRecord")
    @ApiOperation(value = "发放记录-工资发放记录")
    public TableDataInfo<GarmentProductionUserPay> paySalaryRecord(GarmentProductionPayRecordDTO userPayParam) {
        startPage();
        List<GarmentProductionUserPay> list = garmentProductionUserPayService.paySalaryRecord(userPayParam);
        return getDataTable(list);
    }

    @RequiresPermissions("production:pay:export")
    @PostMapping("/paySalaryRecordExport")
    @ApiOperation(value = "发放记录-工资发放记录导出")
    public void paySalaryRecordExport(HttpServletResponse response, @RequestBody GarmentProductionPayRecordDTO userPayParam) {
        List<GarmentProductionUserPay> list = garmentProductionUserPayService.paySalaryRecordExport(userPayParam);
        ExcelUtil<GarmentProductionUserPay> util = new ExcelUtil<GarmentProductionUserPay>(GarmentProductionUserPay.class);
        util.exportExcel(list, "计件工资");
    }


    @RequiresPermissions("production:pay:list")
    @PostMapping("/paySalaryTotal")
    @ApiOperation(value = "发放记录-总发放工资")
    public R paySalaryTotal(GarmentProductionPayRecordDTO userPayParam) {
        return R.ok(garmentProductionUserPayService.paySalaryTotal(userPayParam));
    }


}
